package cn.xiaou.view;




import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.logging.Level;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.mchange.v2.log.ModifyLogLevel;

import cn.xiaou.draw.MyCircleProgressBar;
import cn.xiaou.utils.DataSourceUtils;
import cn.xiaou.utils.ImageManagerUtils;
import cn.xiaou.utils.SmallToolBoxUtils;


public class EnterView extends JFrame {

		private static final long serialVersionUID = 1L;
		// 获取屏幕窗口大小
		public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
	    public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
	    // 定义加载窗口大小
	    public static final int LOAD_WIDTH = 400;
	    public static final int LOAD_HEIGHT = 300;

		private JPanel panel1;


		private MyCircleProgressBar circleProgressBar;

		public EnterView() {
			setIconImage(getToolkit().getImage(ImageManagerUtils.icoUrl));
			setUndecorated(true);
			setBackground(new Color(0,0,0,0));
//			setSize(new Dimension(600, 350));
			setLocation((WIDTH - LOAD_WIDTH) / 2, (HEIGHT - LOAD_HEIGHT) / 2);
	        setSize(LOAD_WIDTH, LOAD_HEIGHT);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			
			ImageIcon background = new ImageIcon(ImageManagerUtils.ProgressBarBg);
			 JLabel bg = new JLabel(background);
			 bg.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
			 JPanel panel = (JPanel) getContentPane();  
			 //设置透明
			 panel.setOpaque(false);
			 //将背景图的label置于窗体的底层
			 getLayeredPane().add(bg, new Integer(Integer.MIN_VALUE)); 
			
			panel1 = new JPanel();
			panel1.setLayout(null);
			panel1.setBounds(0, 0, 400, 500);
			panel1.setOpaque(false);
			circleProgressBar = new MyCircleProgressBar();
//			circleProgressBar.setSize(new Dimension(400, 500));
			circleProgressBar.setBounds(80, 32, 240, 240);
			circleProgressBar.setOpaque(false);
			panel1.add(circleProgressBar);
			setLayout(new GridLayout(1,1));
			add(panel1);
			setVisible(true);
			 Thread t1 = new Thread(new t1(circleProgressBar) );
		        t1.start();
		        try {
					t1.join();
					new LoginView();
					dispose();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		}

		public static void main(String[] args) {
			ModifyLogLevel.modifyInfoLevel(Level.ALL);
			new EnterView();
		}

		

}
class t1 implements Runnable{
	private MyCircleProgressBar circleProgressBar;
	public t1(MyCircleProgressBar circleProgressBar) {
		super();
		this.circleProgressBar = circleProgressBar;
	}
	@Override
	public void run() {
		int index = 0;
		int r = 255;
		int g = 0;
		int b = 0;
		
		while (index <= 100) {
			if(index==20) {
        		try {
					DataSourceUtils.getConnection();
					DataSourceUtils.commitAndRelease();
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null, "服务器连接失败，请检查网络连接");
					System.exit(0);
					e.printStackTrace();
				}
        	}else if(index==40) {
        		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        		try {
					if((Long)(qr.query("select count(*) from version where version=?", new ScalarHandler<>(),SmallToolBoxUtils.version))==0L) {
						JOptionPane.showMessageDialog(null, "有新版本");
						System.exit(0);
					}
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null, "未知错误33057");
					e.printStackTrace();
					System.exit(0);
				}
        	}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (g != 255) {
				g += 5;
			} else {
				r -= 5;
			}
			Color color = new Color(r, g, b);
			circleProgressBar.setForegroundColor(color);
			circleProgressBar.setDigitalColor(color);
			circleProgressBar.setProgress(index++);
		}
		
	}
	
	
}




